﻿using System;
using System.Data;
using System.Text;
using BO.Infrastructure.StaticData;
using BO.Infrastructure.Utils;
using BugManager;

namespace GUI.Controls.News
{
    public partial class GeneralList : System.Web.UI.UserControl
    {
        public string BoxTitle { get; set; }


        internal bool DataBind(DataTable dataTable, int numberOfHotNews = 1)
        {
            try
            {
                if (dataTable != null && dataTable.Rows.Count > 0)
                {
                    var sb = new StringBuilder();

                    for (var i = 0; i < dataTable.Rows.Count; i++)
                    {

                        var r = dataTable.Rows[i];
                        sb.AppendFormat("<li><a href=\"{0}\" title=\"{2}\">{1}{3}</a></li>",
                                        r[SqlConstants.NewsPublish.Url],
                                        r[SqlConstants.NewsPublish.Title],
                                        r[SqlConstants.NewsPublish.EncodedTitle],
                                        numberOfHotNews > 0  ? "<label class=\"hot\"></label>" : string.Empty);
                        numberOfHotNews--;
                    }
                    ltrData.Text = sb.ToString();

                    ltrBoxTitle.Text = BoxTitle;
                    return true;
                }
            }
            catch (Exception ex)
            {
                ErrorManager.Log(ex.ToString());
            }
            return false;
        }

        internal bool DataBind(Utilities.Entities.EmbedThreadGroup embedThreadGroup, int numberOfHotNews = 1)
        {

            try
            {
                if (embedThreadGroup != null && embedThreadGroup.Threads != null && embedThreadGroup.Threads.Length > 0)
                {

                    var sb = new StringBuilder();
                    for (var i = 0; i < embedThreadGroup.Threads.Length; i++)
                    {
                        var r = embedThreadGroup.Threads[i];
                        sb.AppendFormat("<li><a href=\"{0}\" title=\"{2}\">{1}{3}</a></li>",
                                        r.OwnerInfo.Url,
                                        r.OwnerInfo.Name,
                                        PresentationUtils.EnCode(r.OwnerInfo.Name),
                                        numberOfHotNews > 0 ? "<label class=\"hot\"></label>" : string.Empty);
                        numberOfHotNews--;
                    }
                    ltrData.Text = sb.ToString();

                    ltrBoxTitle.Text = BoxTitle;
                    return true;
                }
            }
            catch (Exception ex)
            {
                ErrorManager.Log(ex.ToString());
            }
            return false;
        }
    }
}